home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
17 Bit Software 6: Level 6
/
17 Bit - Level 6 (1998)(Epic Marketing)[!].iso
/
quartz
/
q0040.dms
/
q0040.adf
/
split
/
split.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-11-30
|
3KB
|
128 lines
/* Split a large text file into a set of smaller files */
#include <stdio.h>
#include <stdlib.h>
int lump_size = 1000;
char *pattern;
char *in_file;
char *out_file = "split.";
char out_name[128];
int out_count = 0;
void
doopts(argc,argv)
int argc;
char **argv;
{/* Get the command line options */
int i,got_out;
got_out = 0;
for(i=1;i<argc;i++)
{if(argv[i][0] == '-')
{
switch(argv[i][1])
{
case 'C': case 'c':
out_count = atoi(&argv[i][2]);
break;
case 'L': case 'l':
lump_size = atoi(&argv[i][2]);
break;
case 'p': case 'P':
pattern = &argv[i][2];
break;
default:
/* Tell the user the real set of options */
goto out_options;
}
}
else if(in_file==NULL)
{/* Data file name */
in_file = argv[i];
}
else if(!got_out)
{/* Data file name */
out_file = argv[i];
got_out = ~0;
}
else
{
out_options:
printf("Options are\n\n");
printf(" -l<lump_length>\n");
printf(" -p<text_pattern>\n");
printf(" -c<initial_count>\n");
printf(" <source_file>\n");
printf(" [<out_file>]\n");
exit(20);
}
}
if(in_file==NULL)
goto out_options;
}
main(argc,argv)
int argc;
char **argv;
{/* Split a large file into smaller files */
FILE *in_fptr;
FILE *out_fptr;
int count;
doopts(argc,argv);
/* Open the input file */
in_fptr = fopen(in_file,"r");
if(in_fptr == NULL)
{printf("Cannot open file \"%s\"\n",in_file);
exit(20);
}
count = 0;
while(!feof(in_fptr))
{/* So we must start another file */
sprintf(out_name,"%s%d",out_file,out_count++);
out_fptr = fopen(out_name,"w");
if(out_fptr == NULL)
{printf("Cannot open file \"%s\"\n",out_name);
exit(20);
}
if(pattern)
{/* Matching a pattern, take the simplest approach */
if(count)
fputs(pattern,out_fptr);
count = 0;
while(!feof(in_fptr) && pattern[count]!='\0')
{/* Check out this char this will mess up if we try
to make it case insensitive */
char next;
next = fgetc(in_fptr);
if(pattern[count] == next)
++count;
else if (count)
{int i;
for(i=0;i<count;++i)
fputc(pattern[i],out_fptr);
count = 0;
fputc(next,out_fptr);
}
else
{fputc(next,out_fptr);
}
}
}
else
{/* Just counting out the bytes, should use fread() but
I cannot be bothered */
count = 0;
while(!feof(in_fptr) && count<lump_size)
{fputc(fgetc(in_fptr),out_fptr);
++count;
}
}
fclose(out_fptr);
}
fclose(in_fptr);
}